package com.ken.elmboot.service.impl;

import com.ken.elmboot.common.ElmException;
import com.ken.elmboot.common.Pager;
import com.ken.elmboot.mapper.BaseMapper;
import com.ken.elmboot.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 默认的Service接口实现类
 * @author ken
 * @param <T>
 */
public class DefaultServiceImpl<T> implements BaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public boolean add(T object) {
        boolean ret = false;
        try {
            ret = baseMapper.add(object);
        }catch (Exception e) {
            e.printStackTrace();
            throw new ElmException("新增异常",e);
        }
        return ret;
    }

    @Override
    public boolean deleteById(Integer id) {
        boolean ret = false;
        try {
            ret =  baseMapper.deleteById(id);
        }catch (ElmException e) {
            e.printStackTrace();
            throw new ElmException("删除异常",e);
        }
        return ret;
    }

    @Override
    public boolean edit(T object) {
        boolean ret = false;
        try {
            ret = baseMapper.edit(object);
        }catch (Exception e) {
            e.printStackTrace();
            throw new ElmException("修改异常",e);
        }
        return ret;
    }

    @Override
    public T findById(Integer id) {
        T ret = null;
        try {
            ret = baseMapper.findById(id);
        }catch (Exception e) {
            e.printStackTrace();
            throw new ElmException("查询异常",e);
        }
        return ret;
    }

    @Override
    public int findTotal(Pager<T> pager) {
        int ret = 0;
        try {
            ret = baseMapper.findTotal(pager);
        }catch (Exception e) {
            e.printStackTrace();
            throw new ElmException("查询异常",e);
        }
        return ret;
    }

    @Override
    public List<T> findByPager(Pager<T> pager) {
        List<T> ret = new ArrayList<T>();
        try {
            ret = baseMapper.findByPager(pager);
        }catch (Exception e) {
            e.printStackTrace();
            throw new ElmException("查询异常",e);
        }
        return ret;
    }

    @Override
    public boolean deleteByIds(List<Integer> ids) {
        boolean ret = false;
        try {
            ret = baseMapper.deleteByIds(ids);
        }catch (Exception e) {
            throw new ElmException("删除异常",e);
        }
        return ret;
    }
}
